﻿#region 참조 네임스페이스

using Skying.Reflection.Update;
using System;
using System.Windows.Forms;

#endregion

namespace UpdateTester
{
	public partial class MainForm : Form
	{
		#region 변수

		/// <summary>
		/// 어플리케이션의 업데이트를 담당하는 <see cref="Skying.Reflection.Update.UpdateManager"/> 개체를 나타낸다.
		/// </summary>
		private UpdateManager _updateManager;

		#endregion

		#region 생성자/소멸자

		public MainForm()
		{
			this.InitializeComponent();

			this._updateManager = new UpdateManager();

			this._updateManager.UpdateDownloadStarted = (e) =>
			{
				this.Log("Downloading updates is started.");
			};

			this._updateManager.UpdateDownloadProgressChanged = (e) =>
			{
				this.Log("Update downloaded : {0}%", e.Result);
			};

			this._updateManager.DownloadingUpdateProgressChanged = (e) =>
			{
				this.Log("Update downloaded : {0}, {1}%", e.Result.Gateway.Id, e.Result.DownloadRate);
			};

			this._updateManager.UpdateDownloadCompleted = (e) =>
			{
				this.Log("Downloading updates is completed.");
			};

			this._updateManager.UpdateApplyStarted = (e) =>
			{
				this.Log("Applying updates is started.");
			};

			this._updateManager.UpdateProgressChanged = (e) =>
			{
				
			};

			this._updateManager.UpdateStarted = (e) =>
			{
				this.Log("Updating is started.");

				this.StartButton.Enabled = false;
				this.StopButton.Enabled = true;
			};

			this._updateManager.UpdateCompleted = (e) =>
			{
				this.Log("Updating is completed.");

				this.StartButton.Enabled = true;
				this.StopButton.Enabled = false;

				this._updateManager.RestartApplication();
			};

			this._updateManager.UpdateStopped = (e) =>
			{
				this.Log("Updating is stopped.");

				this.StartButton.Enabled = true;
				this.StopButton.Enabled = false;
			};

			this._updateManager.Error = (e) =>
			{
				this.Log("Updating Error : {0}({1})", e.Result.Message, e.Result.Status);
			};
		}

		#endregion

		#region 로그

		/// <summary>
		/// 텍스트 박스에 로그를 남긴다.
		/// </summary>
		/// <param name="message">로그 메시지를 설정한다.</param>
		public void Log(string message, params object[] args)
		{
			string formattedMessage = string.Format(message, args);

			this.LogTextBox.Text = string.Format("{0} {1}{2}{3}", DateTime.Now.ToString("[yyyy-MM-dd HH:mm:dd,sss]"), formattedMessage, Environment.NewLine, this.LogTextBox.Text);
		}

		#endregion

		#region 버튼 이벤트

		/// <summary>
		/// Start 버튼이 클릭된 경우 발생한다.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void StartButton_Click(object sender, EventArgs e)
		{
			if (!this._updateManager.Initialized)
			{
				this.Log("UpdateManager is not initialized.");

				return;
			}

			this._updateManager.Start(new Uri(this.SchemaUriTextBox.Text), (int)this.RepeatTimeNumericUpDown.Value);
		}

		/// <summary>
		/// Stop 버튼이 클릭된 경우 발생한다.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void StopButton_Click(object sender, EventArgs e)
		{
			this._updateManager.Stop();
		}

		#endregion
	}
}